﻿package com.scn.base.tags;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.log4j.Logger;
import com.scn.base.page.PageVO;
/**
 * 翻页标签      
 *     
 * @author GX       
 * @version 1.0     
 * @created Apr 18, 2011 7:05:39 AM
 */
public final class PaginationLinkTag extends TagSupport { 
	private static Logger  log = Logger.getLogger(PaginationLinkTag.class);
	/**
	 * 翻页对象名称
	 */
	private String page;
	
	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	/**
	 * tlist:header 标签开始
	 *
	 * @exception JspException
	 */
	public int doStartTag() throws JspException {
	  return SKIP_BODY;
	}

	
	
	/**
	 * 从session中读取错误列表
	 *
	 * @exception JspException
	 */
	public int doEndTag() throws JspException
	{
	  JspWriter writer = pageContext.getOut();
	  HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
	  PageVO pagevo=(PageVO)request.getAttribute(page);
	  StringBuffer sb=new StringBuffer();
	  try
	  {
		sb.append("<script language=\"javascript\">")
		  .append("function gotoPageNum(num) {")
		  .append("document.all.currPage.value = num;")
		  .append("document.all.goBtn.click();}")
		  .append("</SCRIPT>");
		//题头部分
		sb.append("<DIV ID=\"PageSelectorMemo\">")
		  .append("页次：")
		  .append(pagevo.getCurPage())
		  .append("/")
		  .append(pagevo.getRecordPages())
		  .append("页")
		  .append("&nbsp;每页显示：")
		  .append(pagevo.getPageSize())
		  .append("条&nbsp;记录数：")
		  .append(pagevo.getRecordCounts())
		  .append("条</DIV>");
		sb.append("<DIV ID=\"PageSelectorSelectorArea\">")
		  .append("<a href=\"javascript:gotoPageNum(1)\" title=\"首页\" style=\"cursor:pointer;\">")
		  .append("<span id=\"PageSelectorHead\">&nbsp;</span>")
		  .append("</a>");
		int harfPage=pagevo.getMaxPages()/2;
		int max=0;
		int min=0;
		if(pagevo.getRecordPages()>pagevo.getMaxPages()){
			if((pagevo.getCurPage()-harfPage)<=0){
				min=1;
				if(pagevo.getMaxPages()>pagevo.getRecordPages()){
					max=pagevo.getRecordPages();
				}else{
					max=pagevo.getMaxPages();
				}
			}else{
				min=pagevo.getCurPage()-harfPage;
				if((pagevo.getMaxPages()+min)>pagevo.getRecordPages()){
					max=pagevo.getRecordPages();
					min=max-pagevo.getMaxPages()+1;
				}else{
					max=pagevo.getMaxPages()+min;
					min=max-pagevo.getMaxPages()+1;
				}
			}
		}else{
			min=1;
			max=pagevo.getRecordPages();
		}		
		for(int t=min;t<=max;t++){
			if(pagevo.getCurPage()==t){
				sb.append("<span id=\"PageSelectorNum\" class=\"PageSelectorSelected\">")
				  .append(t)
				  .append("</span>");
			}else{
				sb.append("<span id=\"PageSelectorNum\" onclick=\"gotoPageNum(")
				  .append(t)
				  .append(");\" style=\"cursor: pointer;\">")
				  .append(t)
				  .append("</span>");
				
			}
		}
		sb.append("<a href=\"javascript:gotoPageNum(")
		.append(pagevo.getRecordPages())
		.append(")\" title=\"尾页\" style=\"cursor:pointer;\">")
		  .append("<span id=\"PageSelectorEnd\">&nbsp;</span>")
		  .append("</a>");
		sb.append("转到：")
		.append("<input id=\"currPage\" class=\"inputStyle\" type=\"text\" name=\"currPage\" value=\"")
		.append(pagevo.getCurPage())
		.append("\" maxlength=\"2\" onfocus=\"this.select();\">")
		.append("<input id=\"goBtn\" class=\"MiddleButtonStyle\" type=\"submit\" name=\"goBtn\" value=\"Go\">")
		.append("</div>");
		writer.write(sb.toString());
		sb.delete(0,sb.length());
		sb=null;
	  }
	  catch (Exception ioe)
	  {
		log.error(ioe.getMessage());
	  }
	  return EVAL_PAGE;
	}

}